/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.flink.fs.obshadoop;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.core.fs.FileSystem;
import org.apache.flink.core.fs.FileSystemFactory;
import org.apache.flink.runtime.util.HadoopConfigLoader;
import org.apache.hadoop.fs.obs.OBSFileSystem;

import java.io.IOException;
import java.net.URI;
import java.util.Collections;
import java.util.Set;

public class FlinkOBSFsFactory implements FileSystemFactory {

    private static final String[] FLINK_CONFIG_PREFIXES = {"fs.obs.", "obs."};

    private static final String HADOOP_CONFIG_PREFIX = "fs.obs.";

    private static final String[][] MIRRORED_CONFIG_KEYS = {};

    private static final Set<String> PACKAGE_PREFIXES_TO_SHADE = Collections.emptySet();

    private static final Set<String> CONFIG_KEYS_TO_SHADE = Collections.emptySet();

    private static final String FLINK_SHADING_PREFIX = "";

    private final HadoopConfigLoader hadoopConfigLoader;

    public FlinkOBSFsFactory() {
        this.hadoopConfigLoader =
            new HadoopConfigLoader(
                FLINK_CONFIG_PREFIXES,
                MIRRORED_CONFIG_KEYS,
                HADOOP_CONFIG_PREFIX,
                PACKAGE_PREFIXES_TO_SHADE,
                CONFIG_KEYS_TO_SHADE,
                FLINK_SHADING_PREFIX);
    }

    @Override
    public void configure(Configuration config) {
        hadoopConfigLoader.setFlinkConfig(config);
    }

    @Override
    public String getScheme() {
        return "obs";
    }

    @Override
    public FileSystem create(URI fsUri) throws IOException {

        // create the Hadoop FileSystem
        org.apache.hadoop.conf.Configuration hadoopConfig =
            hadoopConfigLoader.getOrLoadHadoopConfig();
        hadoopConfig.set("fs.file.impl", "org.apache.hadoop.fs.LocalFileSystem");
        org.apache.hadoop.fs.FileSystem fs = new OBSFileSystem();
        fs.initialize(getInitURI(fsUri, hadoopConfig), hadoopConfig);

        return new FlinkOBSFileSystem(fs);
    }

    protected URI getInitURI(URI fsUri, org.apache.hadoop.conf.Configuration hadoopConfig) {
        final String scheme = fsUri.getScheme();
        final String authority = fsUri.getAuthority();

        if (scheme == null && authority == null) {
            fsUri = org.apache.hadoop.fs.FileSystem.getDefaultUri(hadoopConfig);
        } else if (scheme != null && authority == null) {
            URI defaultUri = org.apache.hadoop.fs.FileSystem.getDefaultUri(hadoopConfig);
            if (scheme.equals(defaultUri.getScheme()) && defaultUri.getAuthority() != null) {
                fsUri = defaultUri;
            }
        }

        return fsUri;
    }
}
